﻿#include "http_client.h"
#include <sstream>
//网络客户端网络请求响应
void HttpClient::on_http_event(mg_connection *connection, int event_type, void *event_data)
{
	http_message *hm = (struct http_message *)event_data;
	int connect_status;

	switch (event_type) 
	{
	case MG_EV_CONNECT:
		connect_status = *(int *)event_data;
		if (connect_status != 0) 
		{
			printf("Error connecting to server, error code: %d\n", connect_status);
			*(int*)connection->mgr->user_data = 1;			
		}
		else {
			printf("connect_status: %d\n", connect_status);
		}
		break;
	case MG_EV_HTTP_REPLY:
	{
		//printf("Got reply:\n%.*s\n", (int)hm->body.len, hm->body.p);
		std::string rsp = std::string(hm->body.p, hm->body.len);
		connection->flags |= MG_F_SEND_AND_CLOSE;		
        *(int*)connection->mgr->user_data = 1;
		on_http_response(rsp);
	}
		break;
	case MG_EV_CLOSE:
		if (*(int*)connection->mgr->user_data == 0) 
		{
			printf("Server closed connection\n");			
			*(int*)connection->mgr->user_data = 1;
		};
		break;
    case MG_EV_HTTP_PART_BEGIN:
    {
        printf("http part begin...\n");
        break;
    }
    case MG_EV_HTTP_PART_DATA:
    {
        printf("http part data...\n");
        break;
    }
    case MG_EV_HTTP_PART_END:
    {
        printf("http part end...\n");
        break;
    }
    case MG_EV_HTTP_MULTIPART_REQUEST_END:
    {
        printf("http multipart request end...\n");
        break;
    }
    //mg_file_upload_handler(c, ev, ev_data, cb);

	default:
		break;
	}
}

void HttpClient::add_post_key_value(string &fmtdata, const string &key, const string &val, const string &boundary)
{
    fmtdata += "--";
    fmtdata += boundary;
    fmtdata += "\r\n";
    fmtdata += "Content-Disposition: form-data; name=\"";
    fmtdata += key;
    fmtdata += "\"\r\n\r\n";
    fmtdata += val;
    fmtdata += "\r\n";
}

void HttpClient::add_post_file(string &fmtdata, const string &key, const string &val, const string &boundary)
{
    fmtdata += "--";
    fmtdata += boundary;
    fmtdata += "\r\n";
    fmtdata += "Content-Disposition: form-data;name=";
    fmtdata += key;
    fmtdata +=";filename=";
    fmtdata += val;
    fmtdata += "\r\n";
    fmtdata += "Content-Type: application/octet-stream\r\n\r\n";
    fmtdata += "hello world";
    fmtdata += get_post_end_data(boundary);
}

string HttpClient::get_protocal_head(const string &content_type, unsigned long long content_len, const string &boundary)
{
    string str = "Content-Type:"+content_type;
    str += ";boundary=";
    str += boundary;
    str += "\r\n";
//    str += "Content-Length:";
//    str += ulltoString(content_len);
//    str += "\r\n";

    return str;
}

string HttpClient::get_post_end_data(const string &boundary)
{
    return "\r\n--" + boundary + "--\r\n";
}

string HttpClient::ulltoString(long long lldata)
{
    string result;
    ostringstream ss;
    ss<<lldata;
    istringstream is(ss.str());
    is>>result;
    return result;
}

void HttpClient::on_http_response(const string res)
{
	printf("http res: %s\n", res.c_str());
}

//发送一次请求，并回调处理,然后关闭本次连接
void HttpClient::send_get_req(const std::string &url)
{	
	mg_mgr mgr;
	int exit_flag = 0;
	mg_mgr_init(&mgr, &exit_flag);
    struct mg_connection * connection = mg_connect_http(&mgr, on_http_event, url.c_str(), NULL, NULL);
	mg_set_protocol_http_websocket(connection);

	//printf("Send http request %s s_exit_flat: %d\n", url.c_str(), exit_flag);		
	while (exit_flag == 0) {
		mg_mgr_poll(&mgr, 500);
	}
		
    mg_mgr_free(&mgr);
}

void HttpClient::send_post_req(const string &url, const string &headers, char *postdata)
{
    mg_mgr mgr;
    int exit_flag = 0;
    mg_mgr_init(&mgr, &exit_flag);
    struct mg_connection * connection = mg_connect_http(&mgr, on_http_event, url.c_str(), headers.c_str(), postdata);
    mg_set_protocol_http_websocket(connection);

    //printf("Send http request %s s_exit_flat: %d\n", url.c_str(), exit_flag);
    while (exit_flag == 0) {
        mg_mgr_poll(&mgr, 500);
    }

    mg_mgr_free(&mgr);
}
